package api

import (
	"github.com/gin-gonic/gin"
	"goadmin/service"
	"net/http"
	"strconv"
)

type ProductController struct {
	svc *service.ProductService
	BaseController
}

// List 商品列表
func (con ProductController) List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10"))
	total, list, err := service.ProductList(page, pageSize)
	if err != nil {
		con.errorMessage(c, "查询失败", 500)
		return
	}
	con.pageSuccess(c, list, total)
}

// Show 商品详情
func (con ProductController) Show(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 64)
	if err != nil {
		con.errorMessage(c, "invalid id", 500)
		return
	}
	p, err := service.GetProduct(id)
	if err != nil {
		con.errorMessage(c, "不存在的产品", 500)
		return
	}
	c.JSON(http.StatusOK, p)
}
